﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace NS.DAL
{
	/// <summary>
	/// 数据访问类:Users
	/// </summary>
	public partial class Users
	{
		public Users()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("UserID", "Users"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int UserID)
		{
			int rowsAffected;
			SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.Int,4)
};
			parameters[0].Value = UserID;

			int result= DbHelperSQL.RunProcedure("ns_Users_Exists",parameters,out rowsAffected);
			if(result==1)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		///  增加一条数据
		/// </summary>
		public int Add(NS.Model.Users model)
		{
			int rowsAffected;
			SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.Int,4),
					new SqlParameter("@Account", SqlDbType.NVarChar,50),
					new SqlParameter("@NickName", SqlDbType.NVarChar,50),
					new SqlParameter("@QQ", SqlDbType.NVarChar,15),
					new SqlParameter("@Password", SqlDbType.NVarChar,100),
					new SqlParameter("@RegTime", SqlDbType.DateTime),
					new SqlParameter("@LastLoginTime", SqlDbType.DateTime),
					new SqlParameter("@RegIP", SqlDbType.NVarChar,20),
					new SqlParameter("@LastLoginIP", SqlDbType.NVarChar,20),
					new SqlParameter("@LoginTimes", SqlDbType.Int,4),
					new SqlParameter("@IsAvailable", SqlDbType.Int,4)};
			parameters[0].Direction = ParameterDirection.Output;
			parameters[1].Value = model.Account;
			parameters[2].Value = model.NickName;
			parameters[3].Value = model.QQ;
			parameters[4].Value = model.Password;
			parameters[5].Value = model.RegTime;
			parameters[6].Value = model.LastLoginTime;
			parameters[7].Value = model.RegIP;
			parameters[8].Value = model.LastLoginIP;
			parameters[9].Value = model.LoginTimes;
			parameters[10].Value = model.IsAvailable;

			DbHelperSQL.RunProcedure("ns_Users_ADD",parameters,out rowsAffected);
			return (int)parameters[0].Value;
		}

		/// <summary>
		///  更新一条数据
		/// </summary>
		public bool Update(NS.Model.Users model)
		{
			int rowsAffected=0;
			SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.Int,4),
					new SqlParameter("@Account", SqlDbType.NVarChar,50),
					new SqlParameter("@NickName", SqlDbType.NVarChar,50),
					new SqlParameter("@QQ", SqlDbType.NVarChar,15),
					new SqlParameter("@Password", SqlDbType.NVarChar,100),
					new SqlParameter("@RegTime", SqlDbType.DateTime),
					new SqlParameter("@LastLoginTime", SqlDbType.DateTime),
					new SqlParameter("@RegIP", SqlDbType.NVarChar,20),
					new SqlParameter("@LastLoginIP", SqlDbType.NVarChar,20),
					new SqlParameter("@LoginTimes", SqlDbType.Int,4),
					new SqlParameter("@IsAvailable", SqlDbType.Int,4)};
			parameters[0].Value = model.UserID;
			parameters[1].Value = model.Account;
			parameters[2].Value = model.NickName;
			parameters[3].Value = model.QQ;
			parameters[4].Value = model.Password;
			parameters[5].Value = model.RegTime;
			parameters[6].Value = model.LastLoginTime;
			parameters[7].Value = model.RegIP;
			parameters[8].Value = model.LastLoginIP;
			parameters[9].Value = model.LoginTimes;
			parameters[10].Value = model.IsAvailable;

			DbHelperSQL.RunProcedure("ns_Users_Update",parameters,out rowsAffected);
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int UserID)
		{
			int rowsAffected=0;
			SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.Int,4)
};
			parameters[0].Value = UserID;

			DbHelperSQL.RunProcedure("ns_Users_Delete",parameters,out rowsAffected);
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string UserIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Users ");
			strSql.Append(" where UserID in ("+UserIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public NS.Model.Users GetModel(int UserID)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.Int,4)
};
			parameters[0].Value = UserID;

			NS.Model.Users model=new NS.Model.Users();
			DataSet ds= DbHelperSQL.RunProcedure("ns_Users_GetModel",parameters,"ds");
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["UserID"]!=null && ds.Tables[0].Rows[0]["UserID"].ToString()!="")
				{
					model.UserID=int.Parse(ds.Tables[0].Rows[0]["UserID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Account"]!=null && ds.Tables[0].Rows[0]["Account"].ToString()!="")
				{
					model.Account=ds.Tables[0].Rows[0]["Account"].ToString();
				}
				if(ds.Tables[0].Rows[0]["NickName"]!=null && ds.Tables[0].Rows[0]["NickName"].ToString()!="")
				{
					model.NickName=ds.Tables[0].Rows[0]["NickName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["QQ"]!=null && ds.Tables[0].Rows[0]["QQ"].ToString()!="")
				{
					model.QQ=ds.Tables[0].Rows[0]["QQ"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Password"]!=null && ds.Tables[0].Rows[0]["Password"].ToString()!="")
				{
					model.Password=ds.Tables[0].Rows[0]["Password"].ToString();
				}
				if(ds.Tables[0].Rows[0]["RegTime"]!=null && ds.Tables[0].Rows[0]["RegTime"].ToString()!="")
				{
					model.RegTime=DateTime.Parse(ds.Tables[0].Rows[0]["RegTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["LastLoginTime"]!=null && ds.Tables[0].Rows[0]["LastLoginTime"].ToString()!="")
				{
					model.LastLoginTime=DateTime.Parse(ds.Tables[0].Rows[0]["LastLoginTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["RegIP"]!=null && ds.Tables[0].Rows[0]["RegIP"].ToString()!="")
				{
					model.RegIP=ds.Tables[0].Rows[0]["RegIP"].ToString();
				}
				if(ds.Tables[0].Rows[0]["LastLoginIP"]!=null && ds.Tables[0].Rows[0]["LastLoginIP"].ToString()!="")
				{
					model.LastLoginIP=ds.Tables[0].Rows[0]["LastLoginIP"].ToString();
				}
				if(ds.Tables[0].Rows[0]["LoginTimes"]!=null && ds.Tables[0].Rows[0]["LoginTimes"].ToString()!="")
				{
					model.LoginTimes=int.Parse(ds.Tables[0].Rows[0]["LoginTimes"].ToString());
				}
				if(ds.Tables[0].Rows[0]["IsAvailable"]!=null && ds.Tables[0].Rows[0]["IsAvailable"].ToString()!="")
				{
					model.IsAvailable=int.Parse(ds.Tables[0].Rows[0]["IsAvailable"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select UserID,Account,NickName,QQ,Password,RegTime,LastLoginTime,RegIP,LastLoginIP,LoginTimes,IsAvailable ");
			strSql.Append(" FROM Users ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" UserID,Account,NickName,QQ,Password,RegTime,LastLoginTime,RegIP,LastLoginIP,LoginTimes,IsAvailable ");
			strSql.Append(" FROM Users ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Users";
			parameters[1].Value = "UserID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

